package Scene5;
import Picture.GameImage;
import Scnen3.Scene3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SnowPanel extends JPanel {


    public JumpAnimal jumpAnimal = new JumpAnimal();
    BG bg = new BG(jumpAnimal);
    Scene3 scene3 = new Scene3();
    Ice1 i1 = new Ice1(jumpAnimal);
    Ice2 i2 = new Ice2(jumpAnimal);
    Ice3 i3 = new Ice3(jumpAnimal);
    Ice4 i4 = new Ice4(jumpAnimal);
    Ice5 i5 = new Ice5(jumpAnimal);
    Ice6 i6 = new Ice6(jumpAnimal);
    startIce si = new startIce(jumpAnimal);
    public int begin = 0;
    public int reopean = 0;
    // TODO
    public int success = 0;



    double force = 0;
    public  void reSize(JFrame frame){
        frame.setSize(1940,1250);
    }
    public SnowPanel(JFrame frame,JButton button){

        jumpAnimal.ice1 = i1;
        jumpAnimal.ice2 = i2;
        jumpAnimal.ice3 = i3;
        jumpAnimal.ice4 = i4;
        jumpAnimal.ice5 = i5;
        jumpAnimal.ice6 = i6;
        jumpAnimal.si = si;
        JButton buttonx = new JButton();


        buttonx.setContentAreaFilled(false);
        buttonx.setBorder(null);
        buttonx.setBounds(860,980,225,97);
        buttonx.addActionListener((e)->{
            reopean = 1;
            jumpAnimal.alive = 1;
            this.remove(buttonx);
            this.requestFocus();
        });

        new Thread(()->{
            while (true){
                if(jumpAnimal.x>2000){
                    success = 1;
                    this.add(button);
                }
                if(jumpAnimal.alive==0){
                    this.add(buttonx);
                }
                if(reopean==1){
                    reopean = 0;
                    jumpAnimal.reOpen();
                    i1.reOpen();
                    i2.reOpen();
                    i3.reOpen();
                    i4.reOpen();
                    i5.reOpen();
                    i6.reOpen();
                    si.reOpen();
                    i6.reOpen();
                    bg.reOpen();
                }
                jumpAnimal.changeImage();
                jumpAnimal.jump();
                i1.move();
                i2.move();
                i3.move();
                i4.move();
                i5.move();
                i6.move();
                si.move();
                i6.changeImage();
                bg.move();

                repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        KeyAdapter adapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code) {
                    case KeyEvent.VK_SPACE:
                        if(force<150)
                        force+=10;
                        System.out.println(jumpAnimal.x);
                        System.out.println(force);
                        break;
                }
                repaint();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                giveForce();

                repaint();
            }

        };
        this.addKeyListener(adapter);
    }
    private void giveForce() {
        jumpAnimal.speed = (int)force;
        if(jumpAnimal.sign==1){
            jumpAnimal.speedx = (int)force;
        }
        i1.speed = (int)force;
        i2.speed = (int)force;
        i3.speed = (int)force;
        i4.speed = (int)force;
        i5.speed = (int)force;
        i6.speed = (int)force;
        si.speed = (int)force;
        bg.speed = (int)force;
        force = 0;
    }

    public void paint(Graphics g){
        super.paint(g);
        g.drawImage(bg.img,bg.x,bg.y,null);
        if(success==1){
            g.drawImage(GameImage.get("/Picture/Scene3/button.png"), 850, 950, null);
            g.drawImage(GameImage.get("/Picture/数字/好耶.png"), 880, 970, null);
            g.drawImage(GameImage.get("/Picture/解救成功.png"),630,300,null);
        }
        if(begin==1&&(jumpAnimal.alive==1||jumpAnimal.y<1250)&&success==0) {
            g.drawImage(i1.img, i1.x, i1.y, null);
            g.drawImage(i2.img, i2.x, i2.y, null);
            g.drawImage(i3.img, i3.x, i3.y, null);
            g.drawImage(i4.img, i4.x, i4.y, null);
            g.drawImage(i5.img, i5.x, i5.y, null);
            g.drawImage(i6.img, i6.x, i6.y, null);
            g.drawImage(si.img, si.x, si.y, null);
            g.drawImage(jumpAnimal.img, jumpAnimal.x, jumpAnimal.y, null);
        }
        if(begin==0){
            g.drawImage(scene3.map.get("提示框"), 650, 150, null);
            g.drawImage(GameImage.get("/Picture/雪花.png"), 600, 210, null);
            g.drawImage(GameImage.get("/Picture/雪花.png"), 600, 410, null);
            g.drawImage(GameImage.get("/Picture/雪花.png"), 600, 610, null);
            g.drawImage(GameImage.get("/Picture/雪花.png"), 600, 810, null);
            g.drawImage(GameImage.get("/Picture/雪花.png"), 600, 1010, null);
            g.drawImage(GameImage.get("/Picture/雪花2.png"), 1200, 210, null);
            g.drawImage(GameImage.get("/Picture/雪花2.png"), 1200, 410, null);
            g.drawImage(GameImage.get("/Picture/雪花2.png"), 1200, 610, null);
            g.drawImage(GameImage.get("/Picture/雪花2.png"), 1200, 810, null);
            g.drawImage(GameImage.get("/Picture/雪花2.png"), 1200, 1010, null);
            g.drawImage(GameImage.get("/Picture/企鹅跳一跳/游戏说明.png"), 850, 360, null);
            g.drawImage(GameImage.get("/Picture/企鹅跳一跳/说明2.png"), 730, 500, null);
            g.drawImage(GameImage.get("/Picture/企鹅跳一跳/提示.png"), 880, 650, null);
            g.drawImage(GameImage.get("/Picture/提示3.png"), 730, 750, null);
            g.drawImage(GameImage.get("/Picture/Bbutton.png"),860,980,null);
            g.drawImage(scene3.map.get("begin"),870,990,null);
        }
        if(jumpAnimal.alive==0&&jumpAnimal.y>=1250){
            g.drawImage(GameImage.get("/Picture/挑战失败.png"),100,100,null);
            g.drawImage(GameImage.get("/Picture/Bbutton.png"),860,980,null);
            g.drawImage(GameImage.get("/Picture/again.png"),870,1000,null);
        }
    }

}
